package net.realsee.service.wcs.task.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.task.WcsTaskTemplate;
import net.realsee.domain.wcs.task.bo.WcsTaskTemplateBo;
import net.realsee.domain.wcs.task.vo.WcsTaskTemplateVo;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.mapper.wcs.task.WcsTaskTemplateMapper;
import net.realsee.service.wcs.task.IWcsTaskTemplateService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 任务模板-主Service业务层处理
 *
 * @author realsee
 * @date 2023-08-11
 */
@RequiredArgsConstructor
@Service
public class WcsTaskTemplateServiceImpl extends ServiceImpl<WcsTaskTemplateMapper, WcsTaskTemplate> implements IWcsTaskTemplateService {

    private final WcsTaskTemplateMapper baseMapper;

    /**
     * 查询任务模板-主
     */
    @Override
    public WcsTaskTemplateVo selectVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询任务模板-主列表
     */
    @Override
    public TableDataInfo<WcsTaskTemplateVo> selectVoPage(WcsTaskTemplateBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsTaskTemplate> lqw = buildVoQueryWrapper(bo);
        Page<WcsTaskTemplateVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询任务模板-主列表
     */
    @Override
    public List<WcsTaskTemplateVo> selectVoList(WcsTaskTemplateBo bo) {
        LambdaQueryWrapper<WcsTaskTemplate> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsTaskTemplate> buildVoQueryWrapper(WcsTaskTemplateBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsTaskTemplate> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, WcsTaskTemplate::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskTempCode()), WcsTaskTemplate::getTaskTempCode, bo.getTaskTempCode());
        lqw.like(StringUtils.isNotBlank(bo.getName()), WcsTaskTemplate::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), WcsTaskTemplate::getDescription, bo.getDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskType()), WcsTaskTemplate::getTaskType, bo.getTaskType());
        lqw.eq(bo.getDisplaySeq() != null, WcsTaskTemplate::getDisplaySeq, bo.getDisplaySeq());
        lqw.eq(StringUtils.isNotBlank(bo.getInterfaceTaskType()), WcsTaskTemplate::getInterfaceTaskType, bo.getInterfaceTaskType());
        lqw.eq(StringUtils.isNotBlank(bo.getVehicleType()), WcsTaskTemplate::getVehicleType, bo.getVehicleType());
        lqw.eq(StringUtils.isNotBlank(bo.getEqptType()), WcsTaskTemplate::getEqptType, bo.getEqptType());
        lqw.eq(StringUtils.isNotBlank(bo.getFuid()), WcsTaskTemplate::getFuid, bo.getFuid());
        lqw.eq(bo.getTextId() != null, WcsTaskTemplate::getTextId, bo.getTextId());
        lqw.eq(bo.getRefrenceId() != null, WcsTaskTemplate::getRefrenceId, bo.getRefrenceId());
        lqw.eq(bo.getUnitId() != null, WcsTaskTemplate::getUnitId, bo.getUnitId());
        lqw.eq(bo.getDelFlag() != null, WcsTaskTemplate::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), WcsTaskTemplate::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, WcsTaskTemplate::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), WcsTaskTemplate::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, WcsTaskTemplate::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteBy()), WcsTaskTemplate::getDeleteBy, bo.getDeleteBy());
        lqw.eq(bo.getDeleteTime() != null, WcsTaskTemplate::getDeleteTime, bo.getDeleteTime());
        lqw.eq(bo.getRowVersionStamp() != null, WcsTaskTemplate::getRowVersionStamp, bo.getRowVersionStamp());
        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), WcsTaskTemplate::getRemark, bo.getRemark());
        return lqw;
    }

    /**
     * 新增任务模板-主
     */
    @Override
    public Boolean insertByBo(WcsTaskTemplateBo bo) {
        WcsTaskTemplate add = BeanUtil.toBean(bo, WcsTaskTemplate.class);
        return this.save(add);
    }

    /**
     * 修改任务模板-主
     */
    @Override
    public Boolean updateByBo(WcsTaskTemplateBo bo) {
        WcsTaskTemplate update = BeanUtil.toBean(bo, WcsTaskTemplate.class);
        return this.updateById(update);
    }

    /**
     * 批量删除任务模板-主
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
